package com.windupurnomo;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.windupurnomo.data.ActivityManagerData;
import com.windupurnomo.entity.ListTask;
import com.windupurnomo.entity.Player;
import com.windupurnomo.entity.ShareInfo;
import com.windupurnomo.entity.TaskDay;
import com.windupurnomo.util.Function;
import com.windupurnomo.util.MyQuery;
import com.windupurnomo.util.Util;

public class Tasks extends ListActivity {
	private ShareInfo shareInfo;
	private TextView txtJam = null;
	private ProgressDialog m_ProgressDialog = null;
	private ArrayList<TaskDay> m_tasks = null;
	private TaskAdapter m_adapter;
	private Runnable viewOrders;
	private ListTask lt = null;
	private int TIME = 0;
	private Player player;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tasks);
		txtJam = (TextView) findViewById(R.id.txtJam);

		m_tasks = new ArrayList<TaskDay>();
		shareInfo = (ShareInfo) getIntent().getSerializableExtra("shareInfo");
		lt = shareInfo.getListTask();
		TIME = shareInfo.getTIME();
		player = shareInfo.getPlayer();
		txtJam.setText(Function.timeFormat(TIME));

		int jumlahTask = lt.getTaskDays().size();
		if (jumlahTask == 0) {
			ActivityManagerData acm = new ActivityManagerData(this);
	        SQLiteDatabase db = acm.getWritableDatabase();
			MyQuery.PlayerDAO.updatePlayer(shareInfo.getPlayer(), db);
			
			shareInfo.setNumFailed(0);
			shareInfo.setPlayerStatus(Util.Player.NEW_PLAYER);
			Intent i = new Intent(this, GameContinue.class);
			i.putExtra("shareInfo", shareInfo);
			startActivity(i);
		}

		this.m_adapter = new TaskAdapter(this, R.layout.row, m_tasks);
		setListAdapter(this.m_adapter);

		viewOrders = new Runnable() {
			public void run() {
				getOrders();
			}
		};
		Thread thread = new Thread(null, viewOrders, "MagentoBackground");
		thread.start();
		m_ProgressDialog = ProgressDialog.show(Tasks.this, "Please wait...",
				"Retrieving data ...", true);
	}

	private Runnable returnRes = new Runnable() {
		public void run() {
			if (m_tasks != null && m_tasks.size() > 0) {
				m_adapter.notifyDataSetChanged();
				for (int i = 0; i < m_tasks.size(); i++)
					m_adapter.add(m_tasks.get(i));
			}
			m_ProgressDialog.dismiss();
			m_adapter.notifyDataSetChanged();
		}
	};

	private void getOrders() {
		try {
			m_tasks = new ArrayList<TaskDay>();
			m_tasks = (ArrayList<TaskDay>) lt.getTaskDays();
			Thread.sleep(0);
		} catch (Exception e) {
		}
		runOnUiThread(returnRes);
	}

	private class TaskAdapter extends ArrayAdapter<TaskDay> {
		private ArrayList<TaskDay> items;

		public TaskAdapter(Context context, int textViewResourceId,
				ArrayList<TaskDay> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row, null);
			}
			TaskDay t = items.get(position);
			if (t != null) {
				TextView tt = (TextView) v.findViewById(R.id.toptext);
				TextView bt = (TextView) v.findViewById(R.id.bottomtext);
				if (tt != null) {
					tt.setText("Task: " + t.getTask().getTaskName());
				}
				if (bt != null) {
					int start = t.getStart();
					int finish = t.getFinish();
					String s, f;
					s = Function.timeFormat(start);
					f = Function.timeFormat(finish);
					String b = "Time: " + s + " - " + f;
					bt.setText(b);
				}
			}
			return v;
		}
	}

	protected void onListItemClick(ListView list, View v, int position, long id) {
		Intent i = new Intent(this, SelectedTask.class);
		long ltId = list.getItemIdAtPosition(position);
		i.putExtra("shareInfo", shareInfo);
		i.putExtra("selectedTask", ltId);// mendapatkan id pemilihan list
		startActivity(i);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.tasks_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		switch (menuItem.getItemId()) {
		case R.id.player: {
			Intent i = new Intent(this, SelectedPlayer.class);
			i.putExtra("shareInfo", shareInfo);
			startActivity(i);
			break;
		}
		case R.id.back: {
			/* reset player resource and condition */
			shareInfo.setPlayer(Function.defaultResConPlayer(player));
			Intent i = new Intent(this, ViewDay.class);
			i.putExtra("shareInfo", shareInfo);
			startActivity(i);
			break;
		}
		}
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
	    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
	    	//Toast.makeText(this, "Menu", Toast.LENGTH_LONG).show();
	        return true;
	    }

	    return super.onKeyDown(keyCode, event);
	}
}